using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesUtilitiesEmployeeMaster : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected DataObj CurPageDataObj = new DataObj();

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState["ResultTable"];
        }

        set
        {
            ViewState["ResultTable"] = value;
        }
    }
    #endregion

    #region GridViewHeader
    protected ArrayList GridViewHeader
    {
        get
        {
            return (ArrayList)ViewState["GridViewHeader"];
        }

        set
        {
            ViewState["GridViewHeader"] = value;
        }
    }
    #endregion

    #region ErrorMessage
    protected string ErrorMessage
    {
        get
        {
            return ViewState["ErrorMessage"].ToString();
        }

        set
        {
            ViewState["ErrorMessage"] = value;
        }
    }
    #endregion

    #region UpdatedMessage
    protected string UpdatedMessage
    {
        get
        {
            return ViewState["UpdatedMessage"].ToString();
        }

        set
        {
            ViewState["UpdatedMessage"] = value;
        }
    }
    #endregion

    #region DeletionFailedMessage
    protected string DeletionFailedMessage
    {
        get
        {
            return ViewState["DeletionFailedMessage"].ToString();
        }

        set
        {
            ViewState["DeletionFailedMessage"] = value;
        }
    }
    #endregion

    #region SAMessage
    protected string SAMessage
    {
        get
        {
            return ViewState["SAMessage"].ToString();
        }

        set
        {
            ViewState["SAMessage"] = value;
        }
    }
    #endregion

    #region DuplicateSAMessage
    protected string DuplicateSAMessage
    {
        get
        {
            return ViewState["DuplicateSAMessage"].ToString();
        }

        set
        {
            ViewState["DuplicateSAMessage"] = value;
        }
    }
    #endregion

    #region EditLink
    protected string EditLink
    {
        get
        {
            return ViewState["EditLink"].ToString();
        }

        set
        {
            ViewState["EditLink"] = value;
        }
    }
    #endregion

    #region DeleteLink
    protected string DeleteLink
    {
        get
        {
            return ViewState["DeleteLink"].ToString();
        }

        set
        {
            ViewState["DeleteLink"] = value;
        }
    }
    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        this.DataDelete();

        if (!IsPostBack)
        {
            this.SwitchLanguage();
            this.PageComponentSetup();

            this.LoadData();
            if (this.ResultTable != null)
            {
                DataView ResultView = new DataView(this.ResultTable);
                ResultView.Sort = this.CurPageDataObj.EmployeeCode + " ASC";
                
                this.BindData(ResultView);
            }
        }
    }
    #endregion

    #region Button_Submit_Click
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        if (this.DataValidate())
        {
            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
            {
                 /*if (CurUserManager.AddAdmin(
                    this.HiddenField_PersonnelInternalID.Value,
                    this.CheckBox_ITAdmin.Checked,
                    this.CheckBox_PowerUser.Checked)) */
                {
                    // log down the history
                    //this.CurLogManager.LogMessage("[GonzalesAdminPanelAdmin] : Admin (" + this.HiddenField_PersonnelInternalID.Value + ", IT Admin: " + this.CheckBox_ITAdmin.Checked + ", Super Admin: " + this.CheckBox_PowerUser.Checked + ") added ", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

                    this.ShowMessage(this.UpdatedMessage);

                    this.LoadData();
                    if (this.ResultTable != null)
                    {
                        DataView ResultView = new DataView(this.ResultTable);
                        ResultView.Sort = this.CurPageDataObj.EmployeeCode + " ASC";

                        this.BindData(ResultView);
                    }
                }
                /*else
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesAdminPanelAdmin.Button_Submit_Click] : Failed at CurUserManager.AddAdmin('" + this.HiddenField_PersonnelInternalID.Value + "', '" + this.CheckBox_ITAdmin.Checked.ToString() + "', '" + this.CheckBox_PowerUser.Checked.ToString() + "') : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.ErrorMessage + LogID);
                } */
            }
        }
    }
    #endregion

    #region OnRowCreated
    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            for (int i = 0; i < this.GridViewHeader.Count; i++)
            {
                cells[i].Text = this.GridViewHeader[i].ToString();
            }
        }
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {

        // browse button
        //this.Button_Browse.Attributes["onclick"] = "ShowModal('AddressBook', document.getElementById('ModalResult')); return false;";
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.lbl_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.lbl_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.lbl_AddNew.Text = CurLanguageFactory.DisplayValue("AddNew");

                this.lbl_EmployeeCode.Text = CurLanguageFactory.DisplayValue("EmployeeCode");
                this.lbl_DisplayName.Text = CurLanguageFactory.DisplayValue("DisplayName");
                this.lbl_ActualName.Text = CurLanguageFactory.DisplayValue("ActualName");
                this.lbl_EmailID.Text = CurLanguageFactory.DisplayValue("EmailID");
                this.lbl_Email.Text = CurLanguageFactory.DisplayValue("Email");
                this.lbl_VendorCode.Text = CurLanguageFactory.DisplayValue("VendorCode");
                this.lbl_CompanyCode.Text = CurLanguageFactory.DisplayValue("CompanyCode");
                this.lbl_CostCenter.Text = CurLanguageFactory.DisplayValue("CostCenter");
                this.lbl_DefualtCurrency.Text = CurLanguageFactory.DisplayValue("DefaultCurrency");
                this.lbl_DateFormat.Text = CurLanguageFactory.DisplayValue("DateFormat");
                this.lbl_Lang.Text = CurLanguageFactory.DisplayValue("Language");

                this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Submit");
                // Create GridView
                this.GridViewHeader = new ArrayList();
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_EmployeeCode"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_DisplayName"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_ActualName"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_EmailID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_Email"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_VendorCode"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_CompanyCode"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_CostCenter"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_DefaultCurrency"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_DateFormat"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("gvh_Language"));

                this.EditLink = CurLanguageFactory.DisplayValue("EditLink");
                this.DeleteLink = CurLanguageFactory.DisplayValue("DeleteLink");
                // default
                this.ErrorMessage = CurLanguageFactory.DisplayValue("ErrorMessage");
                this.UpdatedMessage = CurLanguageFactory.DisplayValue("UpdatedMessage");
                this.DeletionFailedMessage = CurLanguageFactory.DisplayValue("DeletionFailedMessage");
                this.SAMessage = CurLanguageFactory.DisplayValue("SAMessage");
                this.DuplicateSAMessage = CurLanguageFactory.DisplayValue("DuplicateSAMessage");
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesUtilitiesEmployeeMaster.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "ErrorBox";
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {

            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
            {
                SystemUsers AdminPanel = CurUserManager.GetAdminPanel();

                if (AdminPanel != null)
                {

                    this.ResultTable = new DataTable();

                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.EmployeeCode, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DisplayName, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.ActualName, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.EmailID, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.Email, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.VendorCode, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.CompanyCode, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.CostCenter, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.CurrencyDefault, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DateFormat, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.Language, Type.GetType("System.String")));

                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.EditLink, Type.GetType("System.String")));
                    this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DeleteLink, Type.GetType("System.String")));

                    DataRow ResultRow = this.ResultTable.NewRow();
                    this.ResultTable.Rows.Add(ResultRow);

                    /*for (int x = 0; x < AdminPanel.Count; x++)
                    {
                        DataRow ResultRow = this.ResultTable.NewRow();

                        ResultRow[this.CurPageDataObj.AdminID] = AdminPanel[x].InternalID;
                        ResultRow[this.CurPageDataObj.Admin] = AdminPanel[x].DisplayName + " (" + AdminPanel[x].EmailID + ")";
                        ResultRow[this.CurPageDataObj.IsITAdmin] = (AdminPanel[x].IsITAdmin) ? "<img src='" + GonzalesImages.Icon_Checked + "'>" : "";
                        ResultRow[this.CurPageDataObj.IsPowerAdmin] = (AdminPanel[x].IsPowerAdmin) ? "<img src='" + GonzalesImages.Icon_Checked + "'>" : "";
                        ResultRow[this.CurPageDataObj.EditLink] = "<a class='GridViewLink' href='GonzalesAdminPanelAdminDetail.aspx?id=" + AdminPanel[x].InternalID + "' >" + this.EditLink + "</a>";
                        ResultRow[this.CurPageDataObj.DeleteLink] = "<a class='GridViewLink' href='GonzalesAdminPanelAdmin.aspx?DELETE=TRUE&id=" + AdminPanel[x].InternalID + "' onclick=\"return confirm('This action will not able to be undone, continue?');\">" + this.DeleteLink + "</a>";

                        this.ResultTable.Rows.Add(ResultRow);
                    } */
                }
                else
                {
                    this.CurLogManager.LogError("[GonzalesAdminEASDetail.LoadData] : Failed at : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                }
            }
        
    }
    #endregion    

    #region DataDelete
    protected void DataDelete()
    {
        bool smooth = true;

        if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36 &&
            Request.QueryString["DELETE"] != null && Request.QueryString["DELETE"].CompareTo("TRUE") == 0)
        {
            
            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
            {

                if (!(smooth = CurUserManager.DeleteAdmin(Request.QueryString["id"])))
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesUtilitiesEmployeeMaster.DataDelete] : Failed at CurUserManager.DeleteAdmin(Request.QueryString['" + Request.QueryString["id"] + "']) : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.DeletionFailedMessage + LogID);
                }
            }

            if (smooth)
            {
                Response.Redirect("GonzalesUtilitiesEmployeeMaster.aspx");
            }
        }
    }
    #endregion

    #region DataValidate
    protected bool DataValidate()
    {
        bool valid = true;
        /*StringBuilder ErrorMessages = new StringBuilder("<ul>");

        if (this.HiddenField_PersonnelInternalID.Value.Length == 0)
        {
            valid = false;
            ErrorMessages.AppendLine("<li>" + this.SAMessage + "</li>");
        }

        // check if having duplicate entry
        if (this.ResultTable != null)
        {
            foreach (DataRow ResultRow in this.ResultTable.Rows)
            {
                if (ResultRow[this.CurPageDataObj.AdminID].ToString().CompareTo(this.HiddenField_PersonnelInternalID.Value) == 0)
                {
                    valid = false;
                    ErrorMessages.AppendLine("<li>" + this.DuplicateSAMessage + "</li>");
                    break;
                }
            }
        }

        ErrorMessages.AppendLine("</ul>");

        if (!valid) this.ShowErrorMessage(ErrorMessages.ToString());*/

        return valid; 
    }
    #endregion

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

    #region Class: DataObj

    protected class DataObj
    {
        public readonly string EmployeeCode = "user_employeeid";
        public readonly string DisplayName = "user_displayname";
        public readonly string ActualName = "user_login";
        public readonly string EmailID = "user_emailid";
        public readonly string Email = "user_email";
        public readonly string VendorCode = "user_vendorid";
        public readonly string CompanyCode = "user_company";
        public readonly string CostCenter = "user_profit_center";
        public readonly string CurrencyDefault = "user_default_currency";
        public readonly string DateFormat = "user_dateformat";
        public readonly string Language = "user_language";

        public readonly string EditLink = "EditLink";
        public readonly string DeleteLink = "DeleteLink";
    }

    #endregion
}
